public class MyHeap {
    private int[] elem ;
    private int usedSize ;
    public MyHeap(){
        this.elem = new int[10];
    }
    public void display(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(elem[i] + "  ");
        }
    }
    public void initHeap(int[] array){
        for (int i = 0; i < array.length; i++) {
             this.elem[i] = array[i];
             this.usedSize ++ ;
        }
    }

    public void createHeap(){
        for (int parent = ((this.usedSize-1-1)/2); parent >= 0; parent --) {
             shiftDown(parent , this.usedSize);
        }
    }

    private void shiftDown(int parent , int usedSize) {
        int child = parent * 2 + 1 ;
        while(child < usedSize){
            if( child+1 < usedSize && elem[child] < elem[child+1]){
               child ++ ;
            }
            if (elem[child] > elem[parent]) {
                swap(child , parent);
                parent = child ;
                child = parent * 2 + 1 ;
            }else{
                break ;
            }
        }

    }

    private void swap(int child, int parent) {
       int tmp = elem[child];
       elem[child] = elem[parent];
       elem[parent] = tmp ;
    }


}









